//
//  JGDetailViewCell.m
//  JGExpandTableDemo2
//
//  Created by mac on 15/9/21.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGDrupDetailViewCell.h"
#import "JGSubClothes.h"

@interface JGDrupDetailViewCell ()
{
    UIImageView  *_lineView;
    NSMutableArray *_redDotArray;
}
@end

@implementation JGDrupDetailViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = RGB(240.0, 239.0, 244.0);
        
        //上分割线
        _lineView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"gen_line"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)]];
        _lineView.width = self.width;
        [self addSubview:_lineView];
        
        //红点数组
        _redDotArray = [NSMutableArray array];
    }
    
    return self;
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _lineView.top = [JGDrupDetailViewCell tableCellHeight:_infoArray] - 1;
}

/**
 * 设置cell的高度
 */
+(CGFloat)tableCellHeight:(NSArray *)infoArray
{
    if (infoArray.count % 4 == 0)
    {
        return 20 + (infoArray.count / 4) * 90;
    }
    else
    {
        return 20 + (infoArray.count / 4 + 1) * 90;
    }
}


-(void)setInfoArray:(NSArray *)infoArray // 保存着JGSubClothes模型数组
{
    _infoArray = infoArray;
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_redDotArray removeAllObjects];
    
    //取出没一个JGSubClothes模型
    for (int i = 0; i < infoArray.count; i ++)
    {
        //显示图片
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25 + 75 * (i % 4), 20 + (i / 4) * 90, 46, 46)];
        imageView.layer.cornerRadius = 3.0f;
        imageView.clipsToBounds = YES;
        JGSubClothes *subCloth = infoArray[i];
        imageView.image = [UIImage imageNamed:subCloth.imageName];
        [self.contentView addSubview:imageView];
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapItem:)];
        [imageView addGestureRecognizer:tap];
        
        //显示名称
        UILabel *titleLabel = [JGHelper commonLabelWithFrame:CGRectMake(imageView.left - 10, imageView.bottom , imageView.width+ 20, 25) font:[UIFont systemFontOfSize:12.0f] textColor:CommonMainColor lineBreakMode:NSLineBreakByTruncatingTail];
        titleLabel.text = subCloth.name;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:titleLabel];
        
        //红点提示
        UIImageView *redDot = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gen_notification"]];
        redDot.left = 25 + 75 * (i % 4) + 40;
        redDot.top = 17 + (i / 4) * 90;
        [self.contentView addSubview:redDot];
        
#warning mark 控制红点现实的功能并没有去实现
        [_redDotArray addObject:redDot];
    }
}

#pragma mark 点击手势
- (void)tapItem:(UITapGestureRecognizer *)tapGesture
{
    //推送到下一个页面,并根据一些方法去隐藏相应的红点
    
    
}









@end
